package com.youhong.teethcare.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.youhong.teethcare.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 扫描设备适配器
 *
 * @author Administrator
 */
public class ScanDeviceAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> devices;
    private HashMap<BluetoothDevice, Integer> rssis;
    private HashMap<BluetoothDevice, String> names;
    private HashMap<BluetoothDevice, String> devTypes;

    private LayoutInflater mInflator;
    Context context;

    public ScanDeviceAdapter(Context context, ArrayList<BluetoothDevice> device) {
        this.devices = new ArrayList<BluetoothDevice>();
        mInflator = LayoutInflater.from(context);
        this.rssis = new HashMap<BluetoothDevice, Integer>();
        this.names = new HashMap<BluetoothDevice, String>();
        this.devTypes = new HashMap<BluetoothDevice, String>();
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return devices.size();
    }

    @Override
    public BluetoothDevice getItem(int position) {
        // TODO Auto-generated method stub
        return devices.get(position);
    }

    public String getName(BluetoothDevice device) {
        return names.get(device);
    }

    public String getDeviceType(BluetoothDevice device) {
        return devTypes.get(device);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            viewHolder.iv_rssi = view.findViewById(R.id.device_rssi);
            viewHolder.iv_icon = view.findViewById(R.id.device_icon);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = devices.get(position);
        final String deviceName = names.get(device);
        if (deviceName != null && deviceName.length() > 0)
            viewHolder.deviceName.setText(deviceName);

        else
            viewHolder.deviceName.setText("Unkown Device");
        viewHolder.deviceAddress.setText(device.getAddress());
        int rssi = rssis.get(device);
        if (rssis.get(device) > -65) {

            viewHolder.iv_rssi.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_rssi3));

        } else if (rssis.get(device) < -95) {

            viewHolder.iv_rssi.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_rssi1));

        } else {

            viewHolder.iv_rssi.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_rssi2));
        }

        switch (devTypes.get(device).toLowerCase()) {

           /* case "b005":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_b005));
                break;
              case "1751":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1751));

                break;
            case "1770":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1770));
                break;
            case "1668-06":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1668_06));
                break;
            case "1668-05":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1668_05));
                break;
            case "1668":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1668_05));
                break;
            case "1632":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1632));
                break;
            case "1755":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1755));
                break;
            case "1790":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.device_1790));
            */
            case "1741":
                viewHolder.iv_icon.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_tb));
                break;
        }
        return view;
    }

    public void clear() {
        devices.clear();
    }

    public void addDevice(BluetoothDevice device, int rssi, String name, String deviceType) {
        // TODO Auto-generated method stub
        if (!devices.contains(device)) {
            devices.add(device);
            rssis.put(device, rssi);
            names.put(device, name);
            devTypes.put(device, deviceType);
            notifyDataSetChanged();
        }
    }

    static class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
        ImageView iv_icon;
        ImageView iv_rssi;

    }
}
